package com.qn.visitor.controlle;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.qn.common.base.BaseControlle;
import com.qn.common.base.model.ResponseData;
import com.qn.visitor.config.CookieProperties;
import com.qn.common.util.SetCookieUtil;
import com.qn.visitor.util.VisitorUtil;
import com.qn.visitor.model.UserVisitorRequest;
import com.qn.visitor.service.UserAnalysisService;
import com.qn.visitor.vo.AnalyzeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@Tag(name = "访客-JsSdk")
@SaCheckLogin
@RestController
@RequestMapping("JsSdk")
public class JsSdkController extends BaseControlle {
    @DubboReference(version = "1.0.0")
    private UserAnalysisService service;
    @Autowired
    CookieProperties properties;

    @Operation(summary = "GET写入")
    @SaIgnore
    @GetMapping("/probe/")
    public ResponseData<AnalyzeVo> analys(
            @Parameter(name = "did", description = "域名ID") @RequestParam(required = false) Integer did,
            @Parameter(name = "vu", description = "访问地址") @RequestParam(required = false) String vu,
            @Parameter(name = "ru", description = "来源地址") @RequestParam(required = false) String ru,
            @Parameter(name = "tz", description = "用户时区") @RequestParam(required = false) String tz,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        if (vu == null || did == null) {
            return errorD("参数异常");
        }
        //获取用户请求信息
        UserVisitorRequest uvrBo = VisitorUtil.getVisitor(request, did, vu, ru, tz);
        //刷新CK缓存有效期（利用CK种一个Vid特征,有效期90天）
        SetCookieUtil.setVid(response, properties.getCookieDomain(), uvrBo.getCkf());
        //返回数据
        return successD(service.analyze(uvrBo));
    }
}